package com.sysu.minsysu.zdkb;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;

import com.sysu.minsysu.zdkb.utils.Course;

public class FindClassRoomResultActivity extends Activity{

	RoomsStateView[] mRoomsStateViews;
	Gallery mGallery;
	ArrayList<Course>[] mCourseList;
	String[] mBuildingNames; 
	MyGrlleryAdapter mAdapter;
	@Override
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate(savedInstanceState);
		setTitle(activityTitle());
		//ArrayList<Course> courses = (CourseUtils.getClassTimetable(this, "classes.xml"))[2];
		ArrayList<Course> tempList = (ArrayList<Course>)getIntent().getSerializableExtra("courses");
		
		//找到所有的上课地点的名字
		mBuildingNames = getIntent().getStringArrayExtra("buildingNames");
		
		mCourseList = new ArrayList[mBuildingNames.length];
		for( int i=0; i<mCourseList.length; i++ ){
			mCourseList[i] = new ArrayList<Course>();
		}
		
		for( Course course : tempList ){
			for( int i=0; i<mBuildingNames.length; i++ ){
				if( course.getBuilding().equals(mBuildingNames[i]))
					mCourseList[i].add(course);
			}
		}
		//Log.d("Test", mBuildingNames.size()+"");
		mRoomsStateViews = new RoomsStateView[mBuildingNames.length];
		for( int i=0; i<mRoomsStateViews.length; i++ ){
			mRoomsStateViews[i] = new RoomsStateView(this, new int[]{1,15}, mCourseList[i] , mBuildingNames[i], new int[]{5,6,5,5,5});
			mRoomsStateViews[i].setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		}
		
		mGallery = new Gallery(this);
		mAdapter = new MyGrlleryAdapter();
		mGallery.setAdapter(mAdapter);
		mGallery.setHorizontalFadingEdgeEnabled(false);
		mGallery.setSelection( getIntent().getIntExtra("building", 0));
		setContentView( mGallery );
	}
	private String activityTitle(){
		Intent  intent = getIntent();
		int[] date = intent.getIntArrayExtra("date");
		int[] time = intent.getIntArrayExtra("time");
		String campus = intent.getStringExtra("campus");
		String title = campus + " " + date[0] + "-" + date[1] + "-" + date[2] + " 第" + (time[0]+1) + "-" + (time[1]+1)+ "节";
		return title;
	}
	
	
	private class MyGrlleryAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mRoomsStateViews.length;
		}

		@Override
		public Object getItem(int position) {
			return mRoomsStateViews[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return mRoomsStateViews[position];
		}
		
	}
}
